<?php

namespace front\modules\app\controllers;

use Yii;
use front\models\SystemmsgModel;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use messages\services\SystemService;
use yii\helpers\ArrayHelper;
/**
 * SystemmsgController implements the CRUD actions for SystemmsgModel model.
 */
class SystemmsgController extends WebBaseController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all SystemmsgModel models.
     * @return mixed
     */
    public function actionIndex()
    {
        $data = Yii::$app->request->get();
        $data['user_id'] = $this->user_id;
        $result = SystemService::search($data);
        $result['result'] = ArrayHelper::toArray($result['result']);

        $result['result']['list'] = array_map(function($v){
            if($v['type']==1){
                $v['type_name'] = '系统消息';
            }
            if($v['type']==2){
                $v['type_name'] = '优惠消息';
            }
            return $v;
        },$result['result']['list']);

        if($result['code']==1){
            WebBaseController::getAjaxData(2,'操作错误');
        }
        //WebBaseController::P($result['result']);
        WebBaseController::getAjaxData(1,'操作成功',$result['result']);
    }


    /**
     * 是否已读
     */
    function actionIsread(){
        $data['user_id'] =$this->user_id;
        $data['id'] = yii::$app->request->get();
        $result = SystemService::read($data);
        if($result['code']==1){
            WebBaseController::getAjaxData(2,'获取错误');
        }
       WebBaseController::getAjaxData(1,'已读',$result['result']);
    }

    /**
     * Displays a single SystemmsgModel model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new SystemmsgModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new SystemmsgModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing SystemmsgModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing SystemmsgModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the SystemmsgModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SystemmsgModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SystemmsgModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
